$(function () {
    BASE.ANSWER_CONTENT.initMember();
    BASE.ANSWER_CONTENT.getAnswerList(1);
    BASE.ANSWER_CONTENT.scanceQes(1);
    $(document).scroll(function () {
        BASE.COMMON.scrollEvent(BASE.ANSWER_CONTENT.getAnswerList);
    });
    BASE.ANSWER_CONTENT.statusQes(1);
   
});
var BASE = BASE || {};
BASE.ANSWER_CONTENT =(function () {
    var PAGE = 2;
    /*修改问题start************************************************/
    /*修改展示*/
    function modifyPopupShow(obj) {
        var title = $(obj).data('title');
        var content = $(obj).data('content');
        var covers = $(obj).data('covers');
        var categoryid = $(obj).data('categoryid');
        _initCatagoryModify(categoryid);
        _initCoversModify(covers);
        $('#title').val(title);
        BASE.UPLOADANSWER.ue.txt.html(content);
        BASE.POPUP.coversCategory();
        $('#answer_pop').show();
    }
    /*初始化显示分类*/
    function _initCatagoryModify(categoryid) {
        BASE.POPUP.getParentIdByid(categoryid,function (parentId) {
            if(parentId){
                BASE.POPUP.selectInterestPrarent(parentId,function () {
                    $('#interestChildData').val(categoryid);
                    $('#child_'+categoryid).addClass('hover').siblings().removeClass('hover');
                });
            }else {
                BASE.POPUP.selectInterestPrarent(categoryid,function () {
                    $('#interestData').val(categoryid);
                    $('#interestChildData').val('');
                    $('#parent_'+categoryid).addClass('hover').siblings().removeClass('hover');
                });
            }
        });
    }
    /*封面显示初始话*/
    function _initCoversModify(covers) {
        BASE.POPUP.SELECTEDCOVERS = [];
        if(covers){
            var imgs = covers.split(' ');
            imgs.forEach(function (v) {
                BASE.POPUP.SELECTEDCOVERS.push({
                    url:v,
                    id:'',
                });
            });
            $('#selectedCovers').html(template('selectedCovers_tpl',{list:BASE.POPUP.SELECTEDCOVERS}));
        }
    }
    
    /*确认修改*/
    function modifyQuestion(obj) {
        $(obj).attr('disabled',true);
        var sendData = {
            title: $('#title').val(),
            content: BASE.UPLOADANSWER.ue.txt.html(),
            covers: BASE.POPUP.getCovers().join(' '),
            categoryId: $('#interestChildData option:selected').val() ||  $('#interestData option:selected').val(),
            id:BASE.COMMON.getId(),
        };
        if(!sendData.title){
            BASE.clearpop('请输入标题');
            return false;
        }
        if(!sendData.content){
            BASE.clearpop('请输入问题描述');
            return false;
        }
        if(!sendData.categoryId){
            BASE.clearpop('请选择类别');
            return false;
        }
        BASE.postHttp('/device/mutualQuestion/update',sendData,function (res) {
            $(obj).attr('disabled',false);
            if(res.code != 200){
                BASE.clearpop(res.message || '新增失败');
                return;
            }
            $('#answer_pop').hide();
            location.reload();
        })
    }
    /*修改问题end************************************************/
    /*修改回答start************************************************/
    /*回答修改准备*/
    function modifyAnserPrev(obj) {
        var id = $(obj).data('id');
        var content = $(obj).data('content');
        $('body').data('modify_answer_id',id);
        BASE.UPLOAD.ue.txt.html(content);
        BASE.COMMON.scrollTop($('#content').height());
        $('#modify_answer_btn').show();
        $('#and_answer_btn').hide();
    }
    /*确认修改*/
    function modifyAnswer(obj) {
        $(obj).attr('disabled',true);
        var sendData = {
            id:$('body').data('modify_answer_id'),
            content:BASE.UPLOAD.ue.txt.html(),
        };
        if(!sendData.content){
            BASE.clearpop('请输入问题描述');
            return false;
        }
        BASE.postHttp('/device/mutualAnswer/update',sendData,function (res) {
            $(obj).attr('disabled',false);
            if(res.code != 200){
                BASE.clearpop(res.message || '新增失败');
                return;
            }
            BASE.clearpop('修改成功');
            BASE.COMMON.scrollTop(1000);
            $('#modify_answer_btn').hide();
            $('#and_answer_btn').show();
            getAnswerList(1);
            BASE.UPLOAD.ue.txt.html('');
        })
    }
   
    /*删除回答*/
    function removeAnswer(obj) {
        var id = $(obj).data('id');
        BASE.apiPost('/device/mutualAnswer/delete',{id:id},function (res) {
            BASE.clearpop('删除成功');
            getAnswerList(1);
        })
    }
    /*修改回答end************************************************/
    /*浏览*/
    function scanceQes() {
        BASE.apiPost('/device/mutualQuestion/click',{ id: BASE.COMMON.getId() })
    }
    /*点赞*/
    function zanQes(obj) {
        BASE.apiPost('/device/mutualQuestion/like',{ id: BASE.COMMON.getId() },function () {
            $(obj).hide().next().fadeIn();
        })
    }
    /*取消点赞*/
    function unzanQes(obj) {
        BASE.apiPost('/device/mutualQuestion/unlike',{ id: BASE.COMMON.getId() },function () {
            $(obj).hide().prev().fadeIn();
        })
    }
    /*问题状态查询*/
    function statusQes() {
        var sendData = {
            id: BASE.COMMON.getId(),
            needStatus: BASE.COMMON.getUserId() ? true : false,
        };
        BASE.apiGet('/device/mutualQuestion/detail',sendData,function (res) {
            var data = res.statusInfo;
            for(var key in data){
                if(data[key]){
                    $('#'+key+' span:eq(0)').hide();
                    $('#'+key+' span:eq(1)').show();
                }else{
                    $('#'+key+' span:eq(1)').hide();
                    $('#'+key+' span:eq(0)').show();
                }
            }
        })
    }
    /*用户信息获取判断是否关注*/
    function initMember() {
        var userId = BASE.COMMON.getUserId();
        var curId = $('#content').data('memberid');
        if (userId == curId){
            if(! $('#memberModify').hasClass('hide')){
                $('#memberModify').show();
            }
        }else {
            $('#memberNotice').show();
            BASE.COMMON.isNotice($('#memberNotice').data('memberid'),$('#memberNotice'));
        }
    }
    /*新增回答*/
    function addAnswer(obj) {
        var sendData = {
            mutualQuestionId:BASE.COMMON.getId(),
            content:BASE.UPLOAD.ue.txt.html(),
        };
        if(!sendData.content){
            BASE.clearpop('输入您的回答');
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/mutualAnswer/add',sendData,function (res) {
            $(obj).attr('disabled',false);
            if(res.code!==200){
                BASE.clearpop(res.message||'回答失败');
                return;
            }
            BASE.UPLOAD.ue.txt.html('');
            getAnswerList(1);
        })
    }
    /*获取回答列表*/
    function getAnswerList(page) {
        var $box = $('#answerList');
        if(page){
            PAGE = page;
        }
        var sendData = {
            pageNum:PAGE,
            pageSize:10,
            mutualQuestionId:BASE.COMMON.getId(),
            needStatus: BASE.COMMON.getUserId() ? true : false,
        };
        BASE.apiGet('/device/mutualAnswer/list',sendData,function (res) {
            ++PAGE;
            $box.find('.loading').hide();
            BASE.COMMON.SCROLLFLAG = true;
            if((res.nextPage == 0 && res.pages > 1) || res.pages <= 1){
                $box.find('.nomore-data').show();
                BASE.COMMON.SCROLLFLAG = false;
            }
            var tpl = template('answerList_tpl',{data:{
                list:res.list,
                userId:BASE.COMMON.getUserId(),
                questionMemberId:$('#content').data('memberid'),
            }});
            if(sendData.pageNum == 1){
                $box.find('.darenList').html(tpl);
            }else{
                $box.find('.darenList').append(tpl);
            }
            SyntaxHighlighter.all();
        })
    }
    /*点赞一个回答*/
    function zanAnswer(obj) {
        var id = $(obj).data('id');
        var num = $(obj).find('span').text();
        BASE.apiPost('/device/mutualAnswer/like',{ id: id },function () {
            $(obj).hide().next().fadeIn().find('span').text(num/1 + 1);
            $(obj).find('span').text(num/1 + 1);
        })
    }
    /*取消点赞一个回答*/
    function unzanAnswer(obj) {
        var id = $(obj).data('id');
        var num = $(obj).find('span').text();
        num = num -1 < 0 ? 0 : num -1;
        BASE.apiPost('/device/mutualAnswer/unlike',{ id: id },function () {
            $(obj).hide().prev().fadeIn().find('span').text(num);
            $(obj).find('span').text(num);
        })
    }
    /*获取评论回复列表*/
    function getCommentReply(page) {
        if(!page){
            page = 1;
        }
        var $this = $(this);
        var $followList =  $this.parents('li').find('.follow-list');
        var id = $this.data('id') || $('body').data('id');
        $('body').data('id',id);
        var flag = $this.data('flag');
        if(flag){
            $followList.slideToggle();
            return;
        }
        _getCommentData(page,id,$this);
    }
    function _getCommentData(page,id,dom) {
        var sendData = {
            pageNum:page,
            pageSize:15,
            relatedId:id,
            type:3, //类型(1视频 2 bbs，作业 3悬赏4项目5图文6电话7远程8互助9招聘)
        };
        if(BASE.COMMON.getUserId()){
            sendData.needStatus =  true;
        }
        var $followList =  dom.parents('li').find('.follow-list');
        BASE.apiGet('/device/comment/list',sendData,function (res) {
            $followList.find('.darenList').html(template('commentList_tpl',{data:{
                commentList:res.list,
                userId:BASE.COMMON.getUserId(),
            }}));
            if(dom){
                dom.data('flag',true);
            }
            $followList.slideDown();
            paging(res.pageNum,res.pages,getCommentReply,'commentList_'+id);
        })
    }
    /*新增评论*/
    function addComment(obj) {
        var id = $(obj).data('id');
        var sendData = {
            content :$(obj).prev().val(),
            relatedId: id,
            type:3, //类型(1视频 2 bbs，作业，作业 3悬赏4项目5图文6电话7远程8互助9招聘)
        };
        if(!sendData.content){
            BASE.clearpop('请填写评论的内容');
            return;
        }
        $('obj').attr('disabled',true);
        BASE.postHttp('/device/comment/add',sendData,function (res) {
            $('obj').attr('disabled',false);
            if(res.code!=200){
                BASE.clearpop(res.message);
                return;
            }
            BASE.clearpop('评论成功');
            $(obj).prev().val('');
            $(obj).parents('li').find('.slideComment').show();
            var $count = $(obj).parents('li').find('.commentCount');
            $count.text( $count.text()/1 +1);
            _getCommentData(1,id,$(obj));
        })
    }
    /*设置为最佳回答*/
    function setGoodAnswer(obj) {
        var id = $(obj).data('id');
        BASE.apiPost('/device/mutualAnswer/goodAnswer',{id:id},function (res) {
            window.location.reload();
        })
    }
    /*新增回复之后*/
    function addReplyAfter(obj) {
        var $this = $(obj);
        var id = $this.data('id');
        _getCommentData(1,id,$this);
    }
   
    /*所有分享送金币*/
    if(BASE.COMMON.getUserId()){
        $('body').on('click','.share-item',function () {
            BASE.postHttp('/device/mutualQuestion/share',{id:BASE.COMMON.getId()});
        });
        $('body').on('mouseenter','.icon-wechat',function () {
            BASE.postHttp('/device/mutualQuestion/share',{id:BASE.COMMON.getId()});
        });
    }
   
    return {
        modifyPopupShow:modifyPopupShow,
        modifyQuestion:modifyQuestion,
        modifyAnserPrev:modifyAnserPrev,
        modifyAnswer:modifyAnswer,
        removeAnswer:removeAnswer,

        initMember:initMember,
        scanceQes:scanceQes,
        statusQes:statusQes,
        unzanQes:unzanQes,
        zanQes:zanQes,

        addAnswer:addAnswer,
        getAnswerList:getAnswerList,
        getCommentReply:getCommentReply,
        addComment:addComment,
        unzanAnswer:unzanAnswer,
        zanAnswer:zanAnswer,
        setGoodAnswer:setGoodAnswer,
        addReplyAfter:addReplyAfter,
    }
})();
